// page-deviceInformationImg/home.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.requestURL +'upload/uploadImgTest',
    path: '',//文件本地路径
    name: '',//name标识符
    imagesType: '',//自定义上传的目录路径
    identityNum: wx.getStorageSync('identification'),//身份证号
    zhengchesfz: wx.getStorageSync('zhengchesfz'), //7整车和身份证照
    namePlate: wx.getStorageSync('namePlate'), //13整车铭牌和身份证照
    dajiazisfz: wx.getStorageSync('dajiazisfz'), //14大架子（出厂编号）和身份证照
    engine: wx.getStorageSync('engine'), //15发动机铭牌照
    yeyabeng: wx.getStorageSync('yeyabeng'), //16液压泵铭牌照
    bg_identityNum: '',//身份证号
    bg_zhengchesfz: '', //7整车和身份证照
    bg_namePlate: '', //13整车铭牌和身份证照
    bg_dajiazisfz: '', //14大架子（出厂编号）和身份证照
    bg_engine: '', //15发动机铭牌照
    bg_yeyabeng: '', //16液压泵铭牌照
    bigShelfNum: wx.getStorageSync("factoryNum"),//出厂编号
    qylxr: false,
    isSubmit: true
  },
  chooseImage: function (e) {
    console.log(e.target.id)
    var self = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['camera'],
      success: function (res) {
        if (e.target.id == '0') {
          self.setData({
            name: "zhengchesfz",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('zhengchesfz', imgResponseUrl)
            self.setData({
              zhengchesfz: wx.getStorageSync('zhengchesfz')
            })
            if (self.data.zhengchesfz != '') {
              self.setData({
                bg_zhengchesfz: ''
              })
            }
          };
        } else if (e.target.id == '1') {
          self.setData({
            name: "namePlate",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('namePlate', imgResponseUrl)
            self.setData({
              namePlate: wx.getStorageSync('namePlate')
            })
            if (self.data.namePlate != '') {
              self.setData({
                bg_namePlate: ''
              })
            }
          };
        } else if (e.target.id == '2') {
          self.setData({
            name: "dajiazisfz",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('dajiazisfz', imgResponseUrl)
            self.setData({
              dajiazisfz: wx.getStorageSync('dajiazisfz')
            })
            if (self.data.dajiazisfz != '') {
              self.setData({
                bg_dajiazisfz: ''
              })
            }
          };
        } else if (e.target.id == '3') {
          self.setData({
            name: "engine",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('engine', imgResponseUrl)
            self.setData({
              engine: wx.getStorageSync('engine')
            })
            if (self.data.engine != '') {
              self.setData({
                bg_engine: ''
              })
            }
          };
        } else if (e.target.id == '4') {
          self.setData({
            name: "yeyabeng",
            path: res.tempFilePaths,
            imagesType: 'device/' + self.data.identityNum + "/" + self.data.bigShelfNum + '/' + 'mingpai'
          });
          app.uploadimg(self.data);
          app.imgResponseUrlReadyCallback = imgResponseUrl => {
            wx.setStorageSync('yeyabeng', imgResponseUrl)
            self.setData({
              yeyabeng: wx.getStorageSync('yeyabeng')
            })
            if (self.data.yeyabeng != '') {
              self.setData({
                bg_yeyabeng: ''
              })
            }
          };
        }
      }
    });
  },
  submit: function (e) {
    var that = this;
    if (that.data.isSubmit) {
      that.setData({
        isSubmit: false
      })
      var bigShelfNum = that.data.bigShelfNum;//设备编号
      var identityNum = that.data.identityNum;//身份证号
      var identityAndCarImg = that.data.zhengchesfz; //整车和身份证照
      if (identityAndCarImg == null || identityAndCarImg == '') {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var identityAndPlateImg = that.data.namePlate; //整车铭牌和身份证照
      if (bigShelfNum == null || bigShelfNum == '') {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var identityAndBigShelfImg = that.data.dajiazisfz; //大架子号和身份证照
      if (bigShelfNum == null || bigShelfNum == '') {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var enginePlateImg = that.data.engine; //发动机铭牌照
      if (bigShelfNum == null || bigShelfNum == '') {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      var hydraulicPlateImg = that.data.yeyabeng;//液压泵铭牌照
      if (bigShelfNum == null || bigShelfNum == '') {
        wx.showToast({
          title: '照片错误',
          icon: 'loading',
          duration: 2500,
          mask: true
        });
        return false;
      }
      wx.request({
        url: app.globalData.requestURL +'deviceInformationImg/save',
        method: 'POST',
        data: {
          bigShelfNum: bigShelfNum,
          identityNum: identityNum,
          identityAndCarImg: identityAndCarImg,
          identityAndPlateImg: identityAndPlateImg,
          identityAndBigShelfImg: identityAndBigShelfImg,
          enginePlateImg: enginePlateImg,
          hydraulicPlateImg: hydraulicPlateImg
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.status == '0') {
            wx.setStorageSync('zhengchesfz', '');
            wx.setStorageSync('namePlate', '');
            wx.setStorageSync('dajiazisfz', '');
            wx.setStorageSync('engine', '');
            wx.setStorageSync('yeyabeng', '');
            wx.showToast({
              title: '添加成功！',
              icon: 'success',
              duration: 2000,
              mask: true,
              success: function () {
                wx.redirectTo({
                  url: '/page-deviceImg/home',
                })
              }
            });
          } else {
            wx.showToast({
              title: '提交失败！',
              icon: 'loading',
              duration: 2000,
              mask: true,
              success: function () {
                that.setData({
                  isSubmit: true
                })
              }
            });
          }
        }
      })
    } else {
      wx.showToast({
        title: '不可多次提交',
        icon: 'loading',
        duration: 2000,
        mask: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      bigShelfNum: wx.getStorageSync("factoryNum"),
      identityNum: wx.getStorageSync('identification')
    });
    var bigShelfNum = this.data.bigShelfNum
    var identityNum = this.data.identityNum
    if (wx.getStorageSync('zhengchesfz') == null || wx.getStorageSync('zhengchesfz') == '') {
      this.setData({
        bg_zhengchesfz: app.globalData.registerImage[7]
      })
    }
    if (wx.getStorageSync('namePlate') == null || wx.getStorageSync('namePlate') == '') {
      this.setData({
        bg_namePlate: app.globalData.registerImage[13]
      })
    }
    if (wx.getStorageSync('dajiazisfz') == null || wx.getStorageSync('dajiazisfz') == '') {
      this.setData({
        bg_dajiazisfz: app.globalData.registerImage[14]
      })
    }
    if (wx.getStorageSync('engine') == null || wx.getStorageSync('engine') == '') {
      this.setData({
        bg_engine: app.globalData.registerImage[15]
      })
    }
    if (wx.getStorageSync('yeyabeng') == null || wx.getStorageSync('yeyabeng') == '') {
      this.setData({
        bg_yeyabeng: app.globalData.registerImage[16]
      })
    }

    //查询此照片表是否有提交过照片
    wx.request({
      url: app.globalData.requestURL +'deviceInformationImg/TbDeviceInformationImg',
      method: 'POST',
      data: {
        bigShelfNum: bigShelfNum,
        identityNum: identityNum
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        if (res.data.status == '1') {
          wx.redirectTo({
            url: '/page-deviceImg/updataDeviceImg/updataDeviceImg',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})